package cc.rengu.igas.channel.alipay.core.packswitch;

import cc.rengu.jradp.packetswitch.IncomingPackSwitchImpl;
import cc.rengu.jradp.packetswitch.OutgoingPackSwitchImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 与支付宝通讯解包前、解包后、组包前及组后的处理类
 * Created by
 */
public class AliPayClientPackSwitch implements IncomingPackSwitchImpl, OutgoingPackSwitchImpl {

    @Override
    public byte[] beforeInComPackSwitch(byte[] bytes) throws Exception {
        RgLogger rglog = RgLog.getLogger(this.getClass().getName());
        if (null != bytes && new String(bytes).startsWith("{")) {
            String recvMsg = new String(bytes);
            rglog.info("接收的报文内容为：<{}>", recvMsg);
            return bytes;
        } else {
            return "{}".getBytes();
        }
    }

    @Override
    public int afterInComPackSwitch() throws Exception {
        return 0;
    }

    @Override
    public int beforeOutgoPackSwitch() throws Exception {
        return 0;
    }

    @Override
    public byte[] afterOutgoPackSwitch(byte[] bytes) throws Exception {
        RgLogger rglog = RgLog.getLogger(this.getClass().getName());
        if (null == bytes || 2 >= bytes.length) {
            return new byte[0];
        } else {
            String sendMsg = new String(bytes);
            rglog.info("发送的报文内容为：<{}>", sendMsg);
            return bytes;
        }
    }

}
